Partial Class CAL
    Inherits System.Web.UI.Page

#Region " Web Form Designer Generated Code "

    'This call is required by the Web Form Designer.
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()

    End Sub

    'NOTE: The following placeholder declaration is required by the Web Form Designer.
    'Do not delete or move it.
    Private designerPlaceholderDeclaration As System.Object

    Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
        'CODEGEN: This method call is required by the Web Form Designer
        'Do not modify it using the code editor.
        InitializeComponent()
    End Sub

#End Region

    Private Const Q As String = Chr(34)

    Private msTitle As String
    Private msFieldName As String
    Private msDate As String

    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'Put user code to initialize the page here
        If Request.Form.Count = 0 Then
            msTitle = Request.QueryString("T")
            msFieldName = Request.QueryString("FN")
            msDate = Request.QueryString("D")
        Else
            msTitle = Request.Form("T")
            msFieldName = Request.Form("FN")
            msDate = Request.Form("Date")

            If Request.Form("LARROW") > "" Then
                msDate = CDate(msDate).AddMonths(-1).Date.ToString

            ElseIf Request.Form("RARROW") > "" Then
                msDate = CDate(msDate).AddMonths(1).Date.ToString
            End If
        End If
    End Sub

    Public Function PageTitle() As String
        Return msTitle
    End Function

    Public Sub ShowCalendar()
        Call ShowMonth(msDate)
    End Sub

    Private Sub ShowMonth(ByVal sDate As String)
        Dim sFirstDayOfMonth As String
        Dim sLastDayOfMonth As String
        Dim sWeekStart As String
        Dim sToday As String

        Dim iWeek As Integer
        Dim iDay As Integer

        If Not IsDate(sDate) Then
            sDate = FormatDateTime(Now, 2)
        Else
            sDate = FormatDateTime(sDate, 2)
        End If

        'Figure out starting date
        sFirstDayOfMonth = DateAdd("d", (Day(sDate) - 1) * -1, sDate)
        sLastDayOfMonth = DateAdd("d", -1, DateAdd("m", 1, sFirstDayOfMonth))

        If Weekday(sFirstDayOfMonth, vbSunday) > 1 Then
            'First day of month is not first day of week
            sFirstDayOfMonth = DateAdd("d", Weekday(sFirstDayOfMonth) * -1 + 1, sFirstDayOfMonth)
        End If

        If Weekday(sLastDayOfMonth) < 6 Then
            'Last day of month is not last day of week
            sLastDayOfMonth = DateAdd("d", 7 - Weekday(sLastDayOfMonth), sLastDayOfMonth)
        End If

        Response.Write("<form name=" & Chr(34) & "Nav" & Chr(34))
        Response.Write("	method=" & Chr(34) & "Post" & Chr(34))
        Response.Write("	action=" & Chr(34) & "SQCal.aspx" & Chr(34))
        Response.Write("	 runat=" & Chr(34) & "server" & Chr(34))
        Response.Write(">")

        Response.Write("<TABLE width=250 style=" & q & "border: solid 1px gray; border-collapse: seperate;" & q & ">")

        Response.Write("<tr bgcolor=fffffff>")

        Response.Write("<td bgcolor=ccfffff align=right colspan=7>")
        Response.Write("<A HREF=" & Chr(34) & "#" & Chr(34))
        Response.Write("   onClick=" & Chr(34) & "self.close();" & Chr(34))
        Response.Write("   class=Closer")
        Response.Write(">")
        Response.Write("Close")
        Response.Write("</A>")
        Response.Write("</td>")

        Response.Write("</TR>")

        Response.Write("<tr bgcolor=fffffff>")
        Response.Write("<td bgcolor=ccfffff align=left valign=top>")

        Response.Write("<input type=" & Chr(34) & "image" & Chr(34))
        Response.Write("	    src=" & Chr(34) & "images/arrow_left.gif" & Chr(34))
        Response.Write("	  value=" & Chr(34) & "LArrow" & Chr(34))
        Response.Write("	   name=" & Chr(34) & "LArrow" & Chr(34))
        Response.Write("	    alt=" & Chr(34) & "View Previous Month" & Chr(34))
        Response.Write(">")
        Response.Write("</td>")

        Response.Write("<td colspan=5 align=middle class=Month>")
        Response.Write(MonthName(DatePart("m", sDate)) & ", " & DatePart("yyyy", sDate))
        Response.Write("</td>")

        Response.Write("<td bgcolor=ccfffff valign=top align=right>")
        Response.Write("<input type=" & Chr(34) & "image" & Chr(34))
        Response.Write("	    src=" & Chr(34) & "images/arrow_right.gif" & Chr(34))
        Response.Write("	   name=" & Chr(34) & "RArrow" & Chr(34))
        Response.Write("	  value=" & Chr(34) & "RArrow" & Chr(34))
        Response.Write("	    alt=" & Chr(34) & "View Next Month" & Chr(34))
        Response.Write(">")
        Response.Write("</td>")

        Response.Write("<input type=" & Chr(34) & "hidden" & Chr(34))
        Response.Write("	   name=" & Chr(34) & "T" & Chr(34))
        Response.Write("	  value=" & Chr(34) & msTitle & Chr(34))
        Response.Write(">")

        Response.Write("<input type=" & Chr(34) & "hidden" & Chr(34))
        Response.Write("	   name=" & Chr(34) & "Date" & Chr(34))
        Response.Write("	  value=" & Chr(34) & sDate & Chr(34))
        Response.Write(">")

        Response.Write("<input type=" & Chr(34) & "hidden" & Chr(34))
        Response.Write("	   name=" & Chr(34) & "FN" & Chr(34))
        Response.Write("	  value=" & Chr(34) & msFieldName & Chr(34))
        Response.Write(">")

        Response.Write("</tr>")

        Response.Write("<TR bgcolor=ccfffff>")
        Response.Write("<TH width=120 class=DOW>SUN</TH>")
        Response.Write("<TH width=120 class=DOW>MON</TH>")
        Response.Write("<TH width=120 class=DOW>TUE</TH>")
        Response.Write("<TH width=120 class=DOW>WED</TH>")
        Response.Write("<TH width=120 class=DOW>THU</TH>")
        Response.Write("<TH width=120 class=DOW>FRI</TH>")
        Response.Write("<TH width=120 class=DOW>SAT</TH>")

        Dim sWeekURL As String
        sWeekURL = "sucal.aspx?date="

        'Calculate dates for week beginning then
        For iWeek = 0 To DateDiff("ww", sFirstDayOfMonth, sLastDayOfMonth)
            sWeekStart = DateAdd("d", iWeek * 7, sFirstDayOfMonth)

            Response.Write("<TR>")
            For iDay = 0 To 6
                sToday = DateAdd("d", iDay, sWeekStart)

                If CDate(sToday) = CDate(sDate) Then
                    Response.Write("<TD bgcolor=#7fffd4 width=100 valign=top>")
                Else
                    If Month(sToday) <> Month(sDate) Then
                        Response.Write("<TD bgcolor=#dddddd align=left width=100 valign=top>")
                    Else
                        Response.Write("<TD bgcolor=#ffffdf align=left width=100 valign=top>")
                    End If
                End If

                Response.Write("<FONT size=2>")

                Response.Write("<A HREF=" & Chr(34) & sWeekURL & sToday & Chr(34))
                Response.Write("   onClick=" & Chr(34) & "updateValue('" & msFieldName & "','" & sToday & "');" & Chr(34))
                Response.Write("   class=" & Chr(34) & "Day" & Chr(34))
                Response.Write(">")
                Response.Write(Day(sToday))
                Response.Write("</A>")
                Response.Write("</FONT>")
                Response.Write("</TD>")
            Next
            Response.Write("</TR>")
        Next

        Response.Write("</TABLE>")

        Response.Write("</form>")
    End Sub
End Class
